package com.zx.framework.plat.module;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.framework.core.env.Environment;
import com.zx.framework.core.util.BeanUtil;
import com.zx.framework.core.util.FreemarkerUtil;
import com.zx.framework.core.util.gson.GsonUtil;
import com.zx.framework.plat.module.po.Module;
import com.zx.framework.plat.module.vo.ModuleView;

public class ModuleUtil {
	
	public static String readLayout(Module module,List<Module> children){
		String layoutFile = Environment.WEB_INF_REALPATH + "layout" + File.separator + module.getId()+".layout";
		File file = new File(layoutFile);
		if(file.exists()){
			Map context = new HashMap();
			context.put("module", module);
			context.put("children", list2map(children));
			return FreemarkerUtil.getInstance().executeTemplate(context, "layout" + File.separator + module.getId()+".layout");
		}
		else {
			return "";
		}
	}
	
	public static Map<String,ModuleView> list2map(List<Module> modules){
		Map<String,ModuleView> moduleMap = new HashMap<String,ModuleView>();
		for (Module m : modules) {
			ModuleView view = new ModuleView();
			BeanUtil.copyProperties(m, view);
			view.setJson(GsonUtil.object2Json(view));
			moduleMap.put(m.getId(), view);
		}
		return moduleMap;
	}
}
